package com.example.mytips;

import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.event.EditorMouseEvent;
import com.intellij.openapi.editor.event.EditorMouseAdapter;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import org.apache.http.util.TextUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TimerTask;

/**
 * 图片查看工具
 */
public class ImagePopupPlugin {

    public static String startSymbol = "assets";


    private JWindow imagePopup;

    public ImagePopupPlugin() {
        EditorFactory.getInstance().getEventMulticaster().addEditorMouseListener(new EditorMouseAdapter() {
            @Override
            public void mousePressed(EditorMouseEvent e) {
                Editor editor = e.getEditor();
                String selectedText = editor.getSelectionModel().getSelectedText();

                ///如果选中的是assets,或者是以assets打头的完整图片路径
                if (selectedText != null && selectedText.startsWith(startSymbol)) {

                    Project project = editor.getProject();
                    if (project == null) return;

                    String basePath = project.getBasePath();

                    String imgPath = "";
                    if (selectedText.length() == startSymbol.length()) {
                        ///选中的是assets
                        String lineStr = lineStr(e);
                        String lineTrim = lineStr.trim();

                        String replaced = lineTrim.replace(startSymbol, "");
                        if (lineTrim.length() - replaced.length() == startSymbol.length()) {
                            String between1 = findSubstringBetween(lineTrim, startSymbol, "'");
                            String between2 = findSubstringBetween(lineTrim, startSymbol, "\"");
                            System.out.println("between1: " + between1 + ",between2: " + between2);

                            imgPath = between1;
                            if (TextUtils.isEmpty(between1)) {
                                imgPath = between2;
                            }

                            imgPath = basePath + "/" + startSymbol + imgPath;

                        } else {
                            System.out.println("一行包含多个，得用全路径先这中才行");
                        }

                    } else {
                        ///以assets打头的完整图片路径
                        imgPath = basePath + "/" + selectedText;
                    }

                    if (!TextUtils.isEmpty(imgPath)) {
                        showImagePopup(imgPath, e.getMouseEvent().getLocationOnScreen());
                    }
                }
            }
        });
    }

    /// 查找两者之间的字符串
    private static String findSubstringBetween(String mainString, String subString1, String subString2) {
        int index1 = mainString.toLowerCase().indexOf(subString1.toLowerCase());
        int index2 = mainString.toLowerCase().indexOf(subString2.toLowerCase(), index1 + subString1.length());

        if (index1 != -1 && index2 != -1) {
            return mainString.substring(index1 + subString1.length(), index2);
        } else {
            return null;
        }
    }

    /// 光标所在行的内容
    private String lineStr(EditorMouseEvent e) {
        Editor editor = e.getEditor();
        Document document = editor.getDocument();
        CaretModel caretModel = editor.getCaretModel();
        int caretOffset = caretModel.getOffset();
        int lineNumber = document.getLineNumber(caretOffset);
        int lineStartOffset = document.getLineStartOffset(lineNumber);
        int lineEndOffset = document.getLineEndOffset(lineNumber);
        return document.getText(new TextRange(lineStartOffset, lineEndOffset));
    }


    ///展示图片
    private void showImagePopup(String imagePath, Point location) {
        if (imagePopup != null) {
            imagePopup.setVisible(false);
            imagePopup = null;
        }

        ImageIcon imageIcon = new ImageIcon(imagePath);
        JLabel imageLabel = new JLabel(imageIcon);

        imagePopup = new JWindow();
        imagePopup.getContentPane().add(imageLabel);
        imagePopup.pack();
        imagePopup.setLocation(location);
        imagePopup.setVisible(true);

        Timer timer = new Timer(3000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (imagePopup != null) {
                    imagePopup.setVisible(false);
                    imagePopup = null;
                }
                ((Timer) e.getSource()).stop(); // 停止定时器
            }
        });
        timer.setRepeats(false);
        timer.start();
    }
}
